package com.ruoyi.back.service.v3.impl

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.back.domain.v3.StyleSetting
import com.ruoyi.back.domain.v3.dto.StyleSearchDto
import com.ruoyi.back.domain.v3.vo.CartoonStyleVo
import com.ruoyi.back.domain.v3.vo.Pic2PicStyleVo
import com.ruoyi.back.domain.v3.vo.Text2PicStyleVo
import com.ruoyi.back.mapper.StyleSettingMapper
import com.ruoyi.back.service.v3.StyleSettingService
import org.springframework.stereotype.Service

@Service
class StyleSettingServiceImpl : StyleSettingService, ServiceImpl<StyleSettingMapper, StyleSetting>() {


    override fun getNameByIds(ids: String): String? =
        listByIds(ids.split(","))
            .takeIf { it.isNotEmpty() }
            ?.map {
                it.name
            }?.joinToString(",")

    override fun text2PicStyleList(styleSearchDto: StyleSearchDto): Data<List<Text2PicStyleVo>> =
        list(KtQueryWrapper(StyleSetting::class.java)
            .like(StrUtil.isNotBlank(styleSearchDto.name), StyleSetting::name, styleSearchDto.name)
            .eq(StyleSetting::type, 1)
            .orderByDesc(StyleSetting::time))
            .takeIf { it.isNotEmpty() }
            ?.run {
                Ok("获取成功", map {
                    Text2PicStyleVo().apply {
                        BeanUtil.copyProperties(it, this)
                    }
                })
            } ?: Ok("暂无数据", null)

    override fun pic2PicStyleList(styleSearchDto: StyleSearchDto): Data<List<Pic2PicStyleVo>> =
        list(KtQueryWrapper(StyleSetting::class.java)
            .like(StrUtil.isNotBlank(styleSearchDto.name), StyleSetting::name, styleSearchDto.name)
            .eq(StyleSetting::type, 2)
            .orderByDesc(StyleSetting::time))
            .takeIf { it.isNotEmpty() }
            ?.run {
                Ok("获取成功", map {
                    Pic2PicStyleVo().apply {
                        BeanUtil.copyProperties(it, this)
                    }
                })
            } ?: Ok("暂无数据", null)

    override fun cartoonStyleList(styleSearchDto: StyleSearchDto): Data<List<CartoonStyleVo>> =
        list(KtQueryWrapper(StyleSetting::class.java)
            .like(StrUtil.isNotBlank(styleSearchDto.name), StyleSetting::name, styleSearchDto.name)
            .eq(StyleSetting::type, 3)
            .orderByDesc(StyleSetting::time))
            .takeIf { it.isNotEmpty() }
            ?.run {
                Ok("获取成功", map {
                    CartoonStyleVo().apply {
                        BeanUtil.copyProperties(it, this)
                    }
                })
            } ?: Ok("暂无数据", null)


    override fun updateStyle(styleSetting: StyleSetting): Data<Boolean> =
        Ok("修改成功", getById(styleSetting.id)?.run {
            updateById(this.apply {
                BeanUtil.copyProperties(styleSetting, this)
            })
        })
}